# 诊断脚本 - 检查性能数据导出问题

Write-Host "=== 诊断性能数据导出问题 ===" -ForegroundColor Cyan
Write-Host ""

# 1. 检查应用是否在运行
Write-Host "1. 检查应用状态..." -ForegroundColor Yellow
$running = hdc shell "ps -ef | grep vulkantestapp10"
if ($running -match "vulkantestapp10") {
    Write-Host "⚠ 应用仍在运行!" -ForegroundColor Red
    Write-Host "请先停止应用: hdc shell aa force-stop com.huawei.vulkantestapp10" -ForegroundColor Yellow
} else {
    Write-Host "✓ 应用已停止" -ForegroundColor Green
}
Write-Host ""

# 2. 检查可能的文件位置
Write-Host "2. 搜索性能数据文件..." -ForegroundColor Yellow
Write-Host "这可能需要几秒钟..." -ForegroundColor Gray

$locations = @(
    "/data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/",
    "/data/storage/el2/base/haps/entry/files/",
    "/data/local/tmp/",
    "/sdcard/",
    "/data/app/el2/100/base/com.huawei.vulkantestapp10/cache/",
    "/data/app/el2/100/base/com.huawei.vulkantestapp10/temp/"
)

$found = $false
foreach ($loc in $locations) {
    Write-Host "  检查: $loc" -ForegroundColor Gray
    $result = hdc shell "ls $loc 2>/dev/null | grep profiling"
    if ($result) {
        Write-Host "    ✓ 找到文件!" -ForegroundColor Green
        Write-Host "    $result" -ForegroundColor Cyan
        $found = $true
    }
}

if (-not $found) {
    Write-Host "  ✗ 未找到任何 profiling*.csv 文件" -ForegroundColor Red
}
Write-Host ""

# 3. 检查应用数据目录是否存在
Write-Host "3. 检查应用数据目录..." -ForegroundColor Yellow
$appDir = hdc shell "ls -la /data/app/el2/100/base/com.huawei.vulkantestapp10/ 2>&1"
if ($appDir -match "No such file") {
    Write-Host "✗ 应用数据目录不存在!" -ForegroundColor Red
    Write-Host "可能原因:" -ForegroundColor Yellow
    Write-Host "  1. 应用未正确安装" -ForegroundColor Gray
    Write-Host "  2. Bundle name 不正确" -ForegroundColor Gray
    Write-Host "  3. 用户 ID 不是 100" -ForegroundColor Gray
} else {
    Write-Host "✓ 应用数据目录存在" -ForegroundColor Green
    Write-Host "$appDir" -ForegroundColor Gray
}
Write-Host ""

# 4. 检查 files 目录权限
Write-Host "4. 检查 files 目录..." -ForegroundColor Yellow
$filesDir = hdc shell "ls -la /data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/ 2>&1"
if ($filesDir -match "No such file") {
    Write-Host "✗ files 目录不存在" -ForegroundColor Red
    Write-Host "尝试创建目录..." -ForegroundColor Yellow
    hdc shell "mkdir -p /data/app/el2/100/base/com.huawei.vulkantestapp10/haps/entry/files/"
} else {
    Write-Host "✓ files 目录存在" -ForegroundColor Green
    Write-Host "$filesDir" -ForegroundColor Gray
}
Write-Host ""

# 5. 检查最近的日志 (非实时)
Write-Host "5. 检查最近的应用日志..." -ForegroundColor Yellow
Write-Host "获取最近 100 行包含关键字的日志..." -ForegroundColor Gray
$logFile = "recent_logs.txt"
hdc shell "hilog -x | grep -E 'Performance|Exporting|profiling|vulkan' | tail -n 100" > $logFile

if (Test-Path $logFile) {
    $logContent = Get-Content $logFile
    if ($logContent) {
        Write-Host "找到 $($logContent.Count) 条相关日志:" -ForegroundColor Cyan
        $logContent | Select-Object -Last 20 | ForEach-Object {
            Write-Host "  $_" -ForegroundColor Gray
        }
        Write-Host ""
        Write-Host "完整日志已保存到: $logFile" -ForegroundColor Cyan
    } else {
        Write-Host "⚠ 未找到相关日志" -ForegroundColor Yellow
    }
}
Write-Host ""

# 6. 诊断结论
Write-Host "=== 诊断结论 ===" -ForegroundColor Cyan
Write-Host ""

if (-not $found) {
    Write-Host "❌ 问题: 性能数据文件未生成" -ForegroundColor Red
    Write-Host ""
    Write-Host "可能的原因:" -ForegroundColor Yellow
    Write-Host "  1. PerformanceProfiler 未启用" -ForegroundColor Gray
    Write-Host "  2. Cleanup() 函数未被调用" -ForegroundColor Gray
    Write-Host "  3. 文件写入权限不足" -ForegroundColor Gray
    Write-Host "  4. 应用崩溃导致数据未保存" -ForegroundColor Gray
    Write-Host ""
    Write-Host "建议的解决方案:" -ForegroundColor Yellow
    Write-Host "  1. 检查编译是否成功 (是否包含最新代码)" -ForegroundColor Cyan
    Write-Host "  2. 重新编译项目:" -ForegroundColor Cyan
    Write-Host "     .\hvigorw.bat clean" -ForegroundColor Gray
    Write-Host "     .\hvigorw.bat assembleHap" -ForegroundColor Gray
    Write-Host "  3. 重新安装应用" -ForegroundColor Cyan
    Write-Host "  4. 检查应用日志确认 Enable(true) 被调用" -ForegroundColor Cyan
    Write-Host "  5. 确认应用正常退出而非崩溃" -ForegroundColor Cyan
} else {
    Write-Host "✓ 找到性能数据文件" -ForegroundColor Green
    Write-Host "请使用正确的路径拉取文件" -ForegroundColor Cyan
}

Write-Host ""
